const { setCache, getCache } = require("../utils/cache_tool");
const { BaseCommand } = require("./base");

class ChangeFilterCommand extends BaseCommand {
    async execute() {
        const key_name = this.selfData.key_name;
        const status_name = this.selfData.status_name;
        const default_status = this.selfData.default_status;
        const cache_key = `[ChangeFilterCommand]:${this.selfData.cache_key}`;
        const cacheType = this.selfData.cache_type;
        const cacheParam = this.selfData.cache_param;
        const cached = (await getCache(cache_key, cacheType, cacheParam)) || {};
        const ret = this.content.filter((v) => {
            const key = this.getDataByKey(key_name, v);
            const status = this.getDataByKey(status_name, v);
            const cached_status = cached[key];
            cached[key] = status;
            if (default_status != null && status !== default_status)
                return true;
            if (cached_status != null && cached_status !== status) return true;
            return false;
        });
        await setCache(cache_key, cached, cacheType, cacheParam);
        return ret;
    }

    getRequireContent() {
        return true;
    }
}

module.exports = {
    ChangeFilterCommand,
};
